package cn.tedu.csmall.passport.config;

import cn.tedu.csmall.passport.security.JwtAuthenticationFilter;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration

@EnableGlobalMethodSecurity(prePostEnabled = true)//开启通过注解配置权限
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    JwtAuthenticationFilter jwtAuthenticationFilter;

    @Bean
    public PasswordEncoder passwordEncoder()  {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManager()throws Exception{
        return  super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        //禁用防跨域攻击
        http.csrf().disable();
        //URL白名单
        String[] urls={
            "/admins/login","/doc.html","/**/*.js","/**/*.css"
            ,"/favicon.ico","/swagger-resources","/v2/api-docs"

        };
        //配置个请求路径的认证于授权
        http.authorizeRequests()//请求需要授权才可以访问
            .antMatchers(urls)//匹配一些路径
            .permitAll()//允许直接访问（不需要经过认证和授权）
            .anyRequest()//匹配除了以上配置的其他请求
            .authenticated();// 都需要认证
        // 注册处理JWT的过滤器
        // 此过滤器必须在Spring Security处理登录的过滤器之前
        // request ---> Filter ---> DispatcherServlet ---> Interceptor ---> Controller
        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
